﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.Data;

using System.Windows.Controls;
using HA.ModFlow.Controls.Windows.MapData;
using System.Windows;
using HAUtil;
namespace HA.ModFlow.Application
{
    public class ConceptualModelViewModel : MDTVIViewModel
    {
        public ConceptualModelViewModel(MDTVIViewModel parent,ConceptualModel data):base(parent,false)
        {

            this._conceptualModel = data;


            this._contextMenu = new System.Windows.Controls.ContextMenu();
            MenuItem mi = new MenuItem();
            mi.Header = "删除";
            mi.Command = DataCommands.RemoveConceptualModel;
            mi.CommandParameter = data;
            this._contextMenu.Items.Add(mi);

            //mi = new MenuItem();
            //mi.Header = "重命名";

            //this._contextMenu.Items.Add(mi);


            mi = new MenuItem();
            mi.Header = "复制";
            
            
            this._contextMenu.Items.Add(mi);

            Separator sep = new Separator();
            this._contextMenu.Items.Add(sep);

            //二级选项
            mi = new MenuItem();
            mi.Header = "新建图层";
            

            MenuItem innerMi = new MenuItem();
            innerMi.Header = "对象层";
            innerMi.Command = DataCommands.AddCMCoverage;
            innerMi.CommandParameter = new object[] { data, LAYERCOVERAGETYPE.对象层 };
            mi.Items.Add(innerMi);

            innerMi = new MenuItem();
            innerMi.Header = "边界层";
            innerMi.Command = DataCommands.AddCMCoverage;
            innerMi.CommandParameter = new object[] { data, LAYERCOVERAGETYPE.边界层 };
            mi.Items.Add(innerMi);

            innerMi = new MenuItem();
            innerMi.Header = "蒸散层";
            innerMi.Command = DataCommands.AddCMCoverage;
            innerMi.CommandParameter = new object[] { data, LAYERCOVERAGETYPE.蒸散层 };
            mi.Items.Add(innerMi);

            innerMi = new MenuItem();
            innerMi.Header = "补给层";
            innerMi.Command = DataCommands.AddCMCoverage;
            innerMi.CommandParameter = new object[] { data, LAYERCOVERAGETYPE.补给层 };
            mi.Items.Add(innerMi);

            StackPanel sp = new StackPanel();
            sp.Children.Add(new Label() { Content = "水文地质层"});
            sp.Children.Add(new CheckBox() { Content = "层海拔", Margin = new Thickness(25, 0, 0, 0) });
            sp.Children.Add(new CheckBox() { Content = "LPF/BCF参数", Margin = new Thickness(25, 0, 0, 0) });
            sp.Children.Add(new CheckBox() { Content = "UZF参数", Margin = new Thickness(25, 0, 0, 0) });
            sp.Children.Add(new CheckBox() { Content = "溶质运移模型参数", Margin = new Thickness(25, 0, 0, 0) });

            innerMi = new MenuItem();
            innerMi.Header = sp;
            innerMi.Command = DataCommands.AddCMCoverage;
            innerMi.CommandParameter = new object[] { data, LAYERCOVERAGETYPE.水文地质层, sp.Children[1], sp.Children[2], sp.Children[3] ,sp.Children[4] };
            mi.Items.Add(innerMi);

            this._contextMenu.Items.Add(mi);


            mi = new MenuItem();
            mi.Header = "映射有效单元";
            mi.Command = DataCommands.ActiveCellsInCoverage;
            mi.CommandParameter = this._conceptualModel;

            this._contextMenu.Items.Add(mi);

            mi = new MenuItem();
            mi.Header = "映射到";
            //子项
            MenuItem menu = new MenuItem();
            menu.Header = "MODFLOW模型";
            menu.Command = DataCommands.CmMapToModFlow;
            menu.CommandParameter=this._conceptualModel;

            mi.Items.Add(menu);



            menu = new MenuItem();
            menu.Header = "MT3DMS模型";
            menu.Command = DataCommands.CmMapToMT3D;
            menu.CommandParameter = this._conceptualModel;

            mi.Items.Add(menu);



            this._contextMenu.Items.Add(mi);

          

            //menu = new MenuItem();
            //menu.Header = "FEMWATER";
            //mi.Items.Add(menu);

            //menu = new MenuItem();
            //menu.Header = "SEEP2D";
            //mi.Items.Add(menu);

            //menu = new MenuItem();
            //menu.Header = "ADH";
            //mi.Items.Add(menu);
            //this._contextMenu.Items.Add(mi);
            ////子项结束
            //mi = new MenuItem();
            //mi.Header = "Build Horizon Coverages...";
            //this._contextMenu.Items.Add(mi);


            sep = new Separator();
            this._contextMenu.Items.Add(sep);

            MenuItem miProp = new MenuItem();

            miProp.Header = "属性";
            miProp.Command = DataCommands.ConceptualModelProperty;
            miProp.CommandParameter = this._conceptualModel;


            this._contextMenu.Items.Add(miProp);


            this._imageSource = VMImageSource.ConceptModel;

            this.LoadChildren();


            //订阅对应的时间
            this._conceptualModel.NameChanged += new EventHandler(_conceptualModel_NameChanged);



        
    
        }


        void _conceptualModel_NameChanged(object sender, EventArgs e)
        {
            this.OnPropertyChanged("Desc");
        }


      
        ConceptualModel _conceptualModel;

        public ConceptualModel ConceptualModel
        {
            get { return _conceptualModel; }
            set { _conceptualModel = value; }
        }

        public override System.Windows.Input.ICommand DoubleClickCommand
        {
            get
            {

                //目前无法处理带参数的命令，看来参数系统还需要特殊的处理
               
                //return DataCommands.ConceptualModelProperty;

                return new RelayCommand((m) => this.ShowCMProp());

            }
        }

        public void ShowCMProp()
        {

            winConceptualModelProperty cm = new winConceptualModelProperty(this._conceptualModel,ProjectService.DefaultService.Project.Units);
            cm.Owner = GisModule.mainWindow;
            cm.ShowDialog();
 
        }



        public override string Desc
        {
            get
            {
                return HAUtil.LanguageTranslater.ENToCN(this._conceptualModel.Name);
            }
        }


        protected override void LoadChildren()
        {

            foreach (Coverage item in this._conceptualModel.Coverages)
            {
                CoverageViewModel cvm = new CoverageViewModel(this,item);

                this.Children.Add(cvm);
                
            }

            this._conceptualModel.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(_conceptualModel_CollectionChanged);
            
        }



        void _conceptualModel_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {
            switch (e.Type)
            {
                case CollectionChangedType.ADD:

                    CoverageViewModel cvm = new CoverageViewModel(this, (Coverage)e.Child);

                    this.Children.Add(cvm);

                    cvm.IsSelected = true;

                    break;
                case CollectionChangedType.REMOVE:



                    CoverageViewModel cRemove = this.GetCoverageVM((Coverage)e.Child);
                    if (cRemove!=null)
                    {
                        int index = this.Children.IndexOf(cRemove);
                        this.Children.Remove(cRemove);
                        if (index > 0)
                            this.Children[index - 1].IsSelected = true;
                        else if (this.Children.Count > 0)
                            this.Children[0].IsSelected = true;
                    }

                   

                  


                    break;
                case CollectionChangedType.MOVE:
                    break;
                case CollectionChangedType.REPLACE:
                    break;
                case CollectionChangedType.RESET:
                    break;
                default:
                    break;
            }
        }


        protected CoverageViewModel GetCoverageVM(Coverage c)
        {
           
            foreach (CoverageViewModel item in this.Children)
            {
                if (item.DefaultCoverage==c)
                {
                    return item;
                    

                }
                

            }

            return null;

            
 
        }


        protected override void OnSelectedChanged()
        {
            if (this.IsSelected)
            {



                GisModule.DefaultModule.GisManager.InteractiveSystem.CurrentLayer = GisModule.DefaultModule.GisManager.LayerSystem.LayerCollection.GetLayer(this._conceptualModel.Key.ToString()
                    );


                UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.MAPDATA;


            }

        }


        protected override void OnCheckedChanged()
        {


               HAGISV2.Layer layer=GisModule.DefaultModule.GisManager.LayerSystem.LayerCollection.GetLayer(this._conceptualModel.Key.ToString());

           if (layer!=null)
	        {
               layer.Visible=this.IsChecked;
		 
	        }


            base.OnCheckedChanged();
        }
    }
}
